<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

    /*********************************************************
    *            --== InArch ==--
    *
    * INA Init, this in every in solution's heart and soul, it
    * creates links to the database, initializes all the cool
    * classes and prepares configuration files.
    *
    * This file is never to be called directly but rather to be
    * included as a base for any solution. Upon successful execution
    * of the core, subfiles shall have access to the following
    * set of objects/variables
    *
    *  * $db        -- Open Database Handle
    *  * $smarty    -- Smarty Template Parser
    *  * $_CONFIG   -- All Configuration Data
    *  * $myLog     -- Log writer
    * 
    * Please note, that before including the init file, one must define
    * the absolute path to INA in form of a constant INA_ROOT, moreover,
    * as core.inc.php is working within solutions, it also needs access
    * to a constant ROOT, which holds the absolute path to the root
    * of the solution making a call to core.inc.php
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 02.05.2005
    * @copyright essentialmind.com 2005
    *
    * @2do
    *   * handle database connection errors
    *
    *********************************************************/
    
    /**
     * __autoload() -- automatic class loader
     * 
     * @param string $className
     * @return void
     **/
    function __autoload($className){
        /*
            The order of priority lies as follows:
                Solution
                Core
                Core/Nodes
                Core/Ajax
                Core/Admin
                Core/Install
                Core/Cron
            
            Each of the above locations is checked for the existense of
            the desired class. In case none is found the application will
            be halted.
            
            Please note, that external libraries cannot be overridden, thus
            their location in the 'lib' directory
        */
        //first check for an override in the solution's classes dir
        if(file_exists(ROOT.'classes'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once ROOT.'classes'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //now check the core classes repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the nodes repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'client'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'client'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the ajax repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'ajax'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'ajax'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the ajax repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'ajax'.DIRECTORY_SEPARATOR.'client'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'ajax'.DIRECTORY_SEPARATOR.'client'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the admin repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the admin repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        //check the admin repository
        if(file_exists(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'cron'.DIRECTORY_SEPARATOR.$className.'.class.php')){
            require_once INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'cron'.DIRECTORY_SEPARATOR.$className.'.class.php';
            return;
        }
        
        echo "Fatal Error: class \"" . $className . "\" not found!";
        exit;
    }
    
    
    /********************* CONFIGURATION INIT **************************/
    //parse the solution's configuration file and assign it to
    //a global array called $_CONFIG

    try{
        //load default settings
        if(file_exists(INA_ROOT.'ina.ini')){
            $GLOBALS['_CONFIG'] = parse_ini_file(INA_ROOT.'ina.ini');
        }else{ // no joy
            throw new exception('No System Default Configuration Data File Defined (<b>'.INA_ROOT.'ina.ini</b> not found). Can not continue.');
        }

// echo file_exists(ROOT.'ina.ini');
// $handle = fopen(ROOT.'ina.ini', 'r');
// var_dump($handle);
// var_dump(parse_ini_file(ROOT.'ina.ini'));
// exit;

        //check if the solution's ini file is intact
        if(file_exists(ROOT.'ina.ini')){
            //create config array (global access)
            $GLOBALS['_CONFIG'] = array_merge($GLOBALS['_CONFIG'],parse_ini_file(ROOT.'ina.ini'));
        }else{  //no joy
            throw new exception('No Solution Configuration Data File Defined (<b>'.ROOT.'ina.ini</b> not found). Can not continue.');
        }

        //now, add some global system wide settings
        if(file_exists(INA_ROOT.'global.ini')){
            $GLOBALS['_CONFIG'] = array_merge($GLOBALS['_CONFIG'],parse_ini_file(INA_ROOT.'global.ini'));
        }else{ // no joy
            throw new exception('No System Configuration Data File Defined (<b>'.INA_ROOT.'global.ini</b> not found). Can not continue.');
        }
    }catch (exception $e){
        echo $e->getMessage();
        exit;
    }

    
    /*********************** TEMPLATE PARSER ***************************/
    //import Template Parsing Libraries
    require_once INA_ROOT.'lib'.DIRECTORY_SEPARATOR.'smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php';
    
    //import config file reader
    require_once INA_ROOT.'lib'.DIRECTORY_SEPARATOR.'smarty'.DIRECTORY_SEPARATOR.'Config_File.class.php';

    //cast Template Parser
    $smarty = new mySmarty($loadAdmin);
    

    /*********************** DATABASE HANDLER **************************/
    //AdoDB PHP5 extensions
    require_once INA_ROOT.'lib'.DIRECTORY_SEPARATOR.'adodb'.DIRECTORY_SEPARATOR.'adodb-exceptions.inc.php';     
    //AdoDB library
    require_once INA_ROOT.'lib'.DIRECTORY_SEPARATOR.'adodb'.DIRECTORY_SEPARATOR.'adodb.inc.php';                
    //AdoDB session handlers
    require_once INA_ROOT.'lib'.DIRECTORY_SEPARATOR.'adodb'.DIRECTORY_SEPARATOR.'session'.DIRECTORY_SEPARATOR.'adodb-session2.php';    

    /*
        require_once DOC_ROOT.'lib/adodb/session/adodb-session2.php';
        ADOdb_Session::config("mysql", $hostname, $dbuser, $dbpassword, $dbname,$options=false);    
        session_start();
    */

    //configure ADODB libraries
    $ADODB_FETCH_MODE       = ADODB_FETCH_ASSOC;    //fetch associative result sets
    ADOdb_Session::config("postgres8", $_CONFIG['db_host'], $_CONFIG['db_user'], $_CONFIG['db_pass'], $_CONFIG['db_name'],$options=false);  
    

    //try to establish a connection to the database
    //will be deprecated later in favor of smarty cache
    try{
        //create db object
        $db = NewADOConnection("postgres");

		// die ( $_CONFIG['db_host'] . $_CONFIG['db_user'] . $_CONFIG['db_pass'] . $_CONFIG['db_name'] );

        //open a connection
        $db->connect($_CONFIG['db_host'],$_CONFIG['db_user'],$_CONFIG['db_pass'],$_CONFIG['db_name']);  


    }catch(exception $e){   //connection did not succeed
        // echo error contents
        echo "Could not connect to the Database, please verify the connection parameters!";
        exit;
    }
    
    
    /*********************** OTHER INITS ***************************/
    //set the server locale
    setlocale(LC_TIME,$_CONFIG['locale']);
    
    
    /*********************** MAINTANANCE ***************************/
    //check if a pager is present, in case it is not, set it to one
    if(isset($_GET['pg'])){
        $_GET['pg'] = (int)$_GET['pg'];
        if($_GET['pg'] == 0){
            $_GET['pg'] = 1;   
        }
    }else{
        $_GET['pg'] = 1;
    }
    
    //calculate DB limit offset
    $_GET['offset'] = ($_GET['pg'] - 1) * $_CONFIG['perpage'];
    
    
    /*********************** SESSION INIT **************************/
    //make sure that DB session handlers are not persistent
    //thus need to pre_open a session handler
    //adodb_sess_open(false,false,false);
    ADOdb_session::Persist($connectMode=false);
    
    //start the session
    session_start();
 
?>